﻿using System.Dynamic;
using Basic.FormModel;
using Basic.FormModel.AgentForm;
using LinqKit;
using SqlSugar;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.Model;
using WeDonekRpc.SqlSugar;
using WeDonekRpc.SqlSugar.VirtualQuery;

namespace Basic.FormDAL.Repository
{
    internal class AgentFormDAL : IAgentFormDAL
    {
        private readonly IAgentFormRepository _Repository;

        public AgentFormDAL ( IAgentFormRepository repository )
        {
            this._Repository = repository;
        }
        public ExpandoObject Get ( QueryStruct query )
        {
            IVirtualQuery table = this._Repository.GetVirtualTable(query);
            return table.Get();
        }
        public ExpandoObject[] Query ( QueryStruct query, IBasicPage paging, out int count )
        {
            IVirtualQuery table = this._Repository.GetVirtualTable(query);
            return table.Query(paging, out count);
        }
        public ExpandoObject[] Gets ( QueryStruct query )
        {
            IVirtualQuery table = this._Repository.GetVirtualTable(query);
            return table.Gets();
        }
        public ExpandoObject Get ( string tableName, List<IConditionalModel> param )
        {
            return this._Repository.Get(tableName, param);
        }
        public ExpandoObject Get ( string tableName, string[] cols, List<IConditionalModel> param )
        {
            return this._Repository.Get(tableName, param, cols);
        }
        public long Add ( string tableName, Dictionary<string, object> dic )
        {
            long id = IdentityHelper.CreateId();
            dic.Add(ConstDic.KeyColName, id);
            this._Repository.Insert(tableName, dic);
            return id;
        }
        public void CreateTable ( string tableName, string show, AgentTableCol[] cols )
        {
            tableName = char.ToUpper(tableName[0]) + tableName.Substring(1);
            IVirtualTable table = this._Repository.CreateTable(tableName, show);
            cols.ForEach(a =>
            {
                a.Name = char.ToUpper(a.Name[0]) + a.Name.Substring(1);
                table.CreateProperty(a.Name, a.Type, new SqlSugar.SugarColumn
                {
                    Length = a.Len,
                    ColumnDescription = a.Description,
                    ColumnName = a.Name,
                    DecimalDigits = a.Precision,
                    IsPrimaryKey = a.IsOnly,
                    IsNullable = a.IsNull,
                    ColumnDataType = a.DbType,
                    IsJson = a.IsJson
                });
            });
            _ = table.Save();
        }
        public ExpandoObject Get ( string tableName, List<IConditionalModel> where, params string[] col )
        {
            return this._Repository.Get(tableName, where, col);
        }
        public Result[] Gets<Result> ( string tableName, List<IConditionalModel> where, string col )
        {
            return this._Repository.Gets<Result>(tableName, where, col);
        }
        public ExpandoObject[] Gets ( string tableName, List<IConditionalModel> where, string[] cols )
        {
            return this._Repository.Gets(tableName, where, cols);
        }
        public ExpandoObject[] Gets ( string tableName, List<IConditionalModel> where, List<SelectModel> selects )
        {
            return this._Repository.Gets(tableName, where, selects);
        }
        public ExpandoObject[] GroupBy ( string tableName, List<IConditionalModel> where, List<SelectModel> selects, List<GroupByModel> groupBy )
        {
            return this._Repository.GroupBy(tableName, where, selects, groupBy);
        }
        public ExpandoObject Get ( string tableName, List<IConditionalModel> where, List<SelectModel> selects )
        {
            return this._Repository.Get(tableName, where, selects);
        }
        public long Add ( TableAddData add )
        {
            long id = IdentityHelper.CreateId();
            IVirtualQueue queue = this._Repository.BeginQueue();
            Dictionary<string, object> main = add.Column;
            main.Add(ConstDic.KeyColName, id);
            main.Add(ConstDic.CompanyIdColName, add.CompanyId);
            main.Add(ConstDic.LinkPk, add.LinkPk);
            main.Add(ConstDic.LinkType, add.LinkType);
            main.Add(ConstDic.CreateTimeColName, DateTime.Now);
            queue.Insert(add.TableName, main);
            add.Children?.ForEach(c =>
                {
                    Dictionary<string, object> t = c.Column;
                    t.Add(ConstDic.KeyColName, IdentityHelper.CreateId());
                    t.Add(ConstDic.ForeignKeyColName, id);
                    queue.Insert(c.TableName, t);
                });
            _ = queue.Submit();
            return id;
        }

        public bool IsExists ( string table, List<IConditionalModel> where )
        {
            return this._Repository.IsExists(table, where);
        }

        public ExpandoObject[] Query ( string tableName, List<IConditionalModel> where, string[] cols, IBasicPage paging, out int count )
        {
            return this._Repository.Query(tableName, where, cols, paging, out count);
        }
        public ExpandoObject[] Query ( string tableName, IBasicPage paging, out int count )
        {
            return this._Repository.Query(tableName, paging, out count);
        }
    }
}
